﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DynamicProperties.Tests
{
	[TestClass]
	public class DumpTests
	{
		[TestMethod]
		public void DumpEmptyTest()
		{
			object obj = new object();

			// check if dump is empty
			var dump = Extender.Global.DumpObject(obj);
			Assert.AreEqual(0, dump.Length);
		}

		[TestMethod]
		public void DumpBasicTest()
		{
			var testProperty1 = new DynamicProperty<object, int>("Test property 1");
			var testProperty2 = new DynamicProperty<object, int>("Test property 2");
			
			object obj = new object();

			// set property
			int v1 = 123;
			int v2 = 456;
			testProperty1.Set(obj, v1);
			testProperty2.Set(obj, v2);
			
			// check if dump contains correct values
			var dump = Extender.Global.DumpObject(obj);

			// 2 properties
			Assert.AreEqual(2, dump.Length);
			Assert.IsTrue(dump.Any(d => d.Property == testProperty1));
			Assert.IsTrue(dump.Any(d => d.Property == testProperty2));
			Assert.AreEqual(v1, dump.Single(d => d.Property == testProperty1).Value);
			Assert.AreEqual(v2, dump.Single(d => d.Property == testProperty2).Value);
		}

		[TestMethod]
		public void DumpToStringTest()
		{
			var testProperty1 = new DynamicProperty<object, int>("Test property 1");
			
			object obj = new object();

			// set property
			int v1 = 123;
			testProperty1.Set(obj, v1);

			// check if dump contains correct values
			var dump = Extender.Global.DumpObject(obj);

			// 1 property - call ToString
			Assert.AreEqual(1, dump.Length);
			var str = dump[0].ToString();

			// check if text contains text value of important informations
			Assert.IsTrue(str.Contains(v1.ToString()));
			Assert.IsTrue(str.Contains(testProperty1.Description));
		}

		[TestMethod]
		public void DumpResetedTest()
		{
			var testProperty1 = new DynamicProperty<object, int>("Test property 1");
			object obj = new object();
			
			// set property
			testProperty1.Set(obj, 123);
			
			// and reset property
			Extender.Global.ResetProperty(obj, testProperty1);

			// check if dump is empty
			var dump = Extender.Global.DumpObject(obj);
			Assert.AreEqual(0, dump.Length);
		}

		[TestMethod]
		[ExpectedException(typeof(ArgumentNullException))]
		public void DumpNullTest()
		{
			Extender.Global.DumpObject(null);
		}
	}
}
